<template>
  <!-- 最近播放 -->
  <div class="historyPlay">
    <div class="title">
      <div class="left">
        <span class="font-24 font-bold">最近播放</span>
        <span class="font-14 grey mgl-10">共{{historySongs.length}}首</span>
      </div>
      <div class="right grey pointer" @click="clearHistory">清除列表</div>
    </div>
    <div class="btn-wrap mgt-10">
      <button class="btn btn-red" @click="playAll">
      <i class="iconfont icon-bofang"></i>
        播放全部
      </button>
    </div>
    <songs-list :songs="historySongs"></songs-list>
  </div>
</template>

<script>
import SongsList from '@/components/SongsList/songsList.vue'

export default {
  data () {
    return {
      historySongs: []
    }
  },
  created () {
    this.getHistory()
  },
  components: {
    SongsList
  },
  methods: {
    // 获取本地存储中的历史播放列表
    getHistory () {
      this.historySongs = JSON.parse(window.localStorage.getItem('historyList'))
    },
    // 清除历史
    clearHistory () {
      this.$store.commit('setHistory', { type: 'clear' })
      this.getHistory()
    },
    // 播放全部
    playAll () {
      const songIdArr = []
      // 每首歌的id都放进去
      this.historySongs.forEach(item => songIdArr.push(item.id))
      this.$store.commit('playAlbumSong', songIdArr)
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  display: flex;
  justify-content: space-between;
}
.btn-wrap {
  display: flex;
}
</style>
